package Dao.impl;/*
 *86180
 *2021/10/3
 *15:01
 */

import Dao.HealthDao;
import entity.Health;
import entity.Organ;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import util.DruidUtil;
import util.Page;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HealthDaoImpl implements HealthDao {
    private QueryRunner queryRunner=new QueryRunner();
    @Override
    public List<Health> showAll(Page page) {
        String sql="select health.id,projectname,organname,status,deadline,money from health\n" +
                "inner join organ\n" +
                "on health.organId = organ.id limit ?,?;";

        try {
            List<Map<String, Object>> query = queryRunner.query(DruidUtil.getConnection(), sql, new MapListHandler(), page.getStartRow(), page.getPageSize());
            if(query==null){
                return null;
            }
            List<Health> healthList=new ArrayList<>();
            for(Map<String,Object> maps:query){
                Health health=new Health();
                Organ organ=new Organ();

                BeanUtils.populate(health, maps);
                BeanUtils.populate(organ, maps);
                health.setOrgan(organ);
                healthList.add(health);
            }
            return healthList;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage()+"查询health综合表异常");
        }
    }

    @Override
    public Long selectCount() {
        try {
            return queryRunner.query(DruidUtil.getConnection(), "select count(*) from health;",new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"查询总条数出现异常");
        }
    }

    @Override
    public int insert(Health health) {
        Object [] params={health.getProjectname(),health.getStatus(),health.getDeadline(),health.getMoney(),health.getOrganId()};
        try {
            return queryRunner.update(DruidUtil.getConnection(), "insert into health values(0,?,?,?,?,?);",params);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"新增health异常");
        }
    }

    @Override
    public int update(Health health) {
        Object [] params={health.getProjectname(),health.getStatus(),health.getDeadline(),health.getMoney(),health.getOrganId(),health.getId()};
        try {
            return queryRunner.update(DruidUtil.getConnection(), "update health set projectname=?,status=?,deadline=?,money=?,organId=? where id=?;",params);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"修改health异常");
        }
    }

    @Override
    public Health select(Integer id) {
        String sql="select health.id,projectname,organname,status,organId,deadline,money from health\n" +
                "inner join organ\n" +
                "on health.organId = organ.id where health.id=?;";
        try {
            Map<String, Object> query = queryRunner.query(DruidUtil.getConnection(), sql, new MapHandler(),id);
            if(query==null){
                return null;
            }
            Health health=new Health();
            Organ organ=new Organ();
            BeanUtils.populate(health, query);
            BeanUtils.populate(organ, query);
            health.setOrgan(organ);
            return health;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage()+"查询单条数据异常");
        }
    }

    @Override
    public int delect(Integer id) {
        try {
            return queryRunner.update(DruidUtil.getConnection(), "delete from health where id=?;",id);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage()+"删除异常");
        }
    }
}
